package cn.wangyu.msblogadmin.common.utils;


import cn.wangyu.msblogadmin.common.result.ResponseCode;
import cn.wangyu.msblogadmin.common.result.ResultModel;

import java.util.Collection;

/**
 * @author wangyu
 * @version 1.0
 * @date 2021/4/11 23:20
 */
public class ResultUtil {
    /**
     * @return
     */
    public static ResultModel success() {
        return new ResultModel(ResponseCode.SUCCESS.getDesc(), null,ResponseCode.SUCCESS.getCode());
    }

    /**
     * 构造http返回信息
     */
    public static <T> ResultModel success(Collection<T> entities,
                                            String... properties) {
        ResultModel result = new ResultModel(ResponseCode.SUCCESS.getDesc(), null,ResponseCode.SUCCESS.getCode());

        if (entities == null || entities.isEmpty() || properties == null) {
            result.setData(entities);
            return result;
        }

    /*for (T entity : entities) {
      filterProperties(entity, properties);
    }*/

        result.setData(entities);
        return result;
    }

    /**
     * 构造http返回信息
     */
    public static <T> ResultModel success(Iterable<T> page) {
        ResultModel result = new ResultModel(ResponseCode.SUCCESS.getDesc(), null,ResponseCode.SUCCESS.getCode());

        if (page == null) {
            result.setData(page);
            return result;
        }

        //TODO
        result.setData(page);
        return result;
    }

    /**
     * 构造http返回信息
     *
     * @param entity     返回的业务处理结果
     */
    public static <T> ResultModel success(T entity) {
        ResultModel result = new ResultModel(ResponseCode.SUCCESS.getDesc(), null,ResponseCode.SUCCESS.getCode());
        if (entity == null) {
            return result;
        }

        // TODO 封装转换

        result.setData(entity);
        return result;
    }

    /**
     * 参数校验错误时返回信息
     *
     * @param responseCode 状态码
     * @param message      错误信息
     */
    public static ResultModel error(ResponseCode responseCode,
                                      String message) {
        ResultModel ResultModel = new ResultModel(message, null, responseCode.getCode());
        return ResultModel;
    }

    /**
     * 参数校验错误时返回信息
     *
     * @param responseCode 状态码
     */
    public static ResultModel error(ResponseCode responseCode) {
        ResultModel ResultModel = new ResultModel(responseCode.getDesc(), null,responseCode.getCode());
        return ResultModel;
    }
}
